package cn.edu.scnu.user.controller;

import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.pojo.User;
import cn.edu.scnu.user.service.RelationService;
import cn.edu.scnu.user.service.UserService;
import cn.edu.scnu.vo.Card;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 用户之间的关注粉丝相关功能
 */
@RestController
@Api
public class RelationController {
    @Autowired
    private RelationService relationService;

    @Autowired
    private UserService userService;

    @IgnoreToken
    @GetMapping("/users/{username}/lists")
    @ApiOperation("获取用户关注或粉丝列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(
                            name = "username",
                            value = "用户名",
                            dataType = "String",
                            required = true
                    ),
                    @ApiImplicitParam(
                            name = "type",
                            value = "类型",
                            dataType = "String",
                            required = true,
                            type = "query"
                    )
            }
    )
    public SysResult getLists(@PathVariable String username, String type, HttpServletRequest request) throws IOException {
        // 验证参数type
        if (!(type.equals("follows") || type.equals("fans"))) {
            return SysResult.build(ResultCode.FAIL, "错误的参数", null);
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = null;
        try {
            user = (User) authentication.getPrincipal();
        } catch (NullPointerException e) {
        }
        List<Card> data = null;
        data = relationService.getLists(user, username, type);
        return SysResult.build(ResultCode.SUCCESS, "success", data);
    }

    @PatchMapping("/users/follows/{username}")
    @ApiOperation("关注用户")
    @ApiImplicitParam(
            name = "username",
            value = "关注的用户",
            dataType = "String",
            required = true
    )
    public SysResult follow(@PathVariable String username, HttpServletRequest request) throws IOException {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        relationService.follow(user, username);
        return SysResult.build(ResultCode.CREATED, "修改成功", null);
    }


}
